package com.lzh.others.java8;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: stream()中的 mapToInt(ToIntFunction mapper)返回一个IntStream其中包含给定函数应用于此流得元素的结果
 * maptoint有sum()求和方法
 * 使用场景，对集合某一个对象求和
 * //TODO stream()中的map(Function mapping) 和它的区别呢
 * @Author: LiuZiHao
 * @Date: 2022/7/14 16:12
 */
public class MapToInt {
    private static final String[] PARTIAL_DEVICE_CATEGORIES = {"飞机牵引车", "加油车", "供水车", "空调车", "升降平台车", "摆渡车", "气源车", "电源车", "旅客舷梯车", " 自动升降登机车", "特种运货车", "食品车", "污水车", "清扫车", "吹雪车", "除胶车", "道面摩擦系数测试车", "消防车", "其它"};

    public static void main(String[] args) {
        String location = "机场";
        String type = "飞机";
        if ("机场".equals(location) && Arrays.asList(PARTIAL_DEVICE_CATEGORIES).contains(type)) {
            System.out.println("校验通过");
        }
        Integer zero = 0;
        if (zero.equals(null)) {
            System.out.println("test");
        }
        List<SysUser> list = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            SysUser a = new SysUser();
            a.setAge(5);
            if (i == 4) {
                a.setAge(null);
            }
            list.add(a);
        }
        // 空指针
        try {
            int test = list.stream().mapToInt(SysUser::getAge).sum();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 正确写法
//        int sum = list.stream().mapToInt(o -> Objects.isNull(o.getAge()) ? 0 : o.getAge()).sum();
        int sum = list.stream().mapToInt(o -> o.getAge() == null ? 0 : o.getAge()).sum();
        System.out.println(sum);
    }

}
